﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EHTSaleManager.Code.Entity;
using EHTSaleManager.Code.BLL;
using System.Data;
using System.Web.Profile;

namespace EHTSaleManager.UICustomer
{
    public partial class CustomerComfirmDeleteOrder : System.Web.UI.Page
    {
        public Int32 OrderID = -1;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["OrderID"] != null)
            {
                try
                {
                    OrderID = Int32.Parse(Request.QueryString["OrderID"].ToString());
                }
                catch (Exception ex)
                {
                    OrderID = -1;
                }
            }

            if (OrderID != -1)
            {
                Order theOrder = OrderProvider.GetOrderByOrderID(OrderID);
                this.ltrMessage.Text = "Are you sure delete this Order?";
                ViewState["ID"] = theOrder.OrderID;
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (ViewState["ID"] != null)
            {
                OrderID = Int32.Parse(ViewState["ID"].ToString());
                Order theOrder = OrderProvider.GetOrderByOrderID(OrderID);
                float fServiceCost = 0;
                if (theOrder != null)
                {
                    fServiceCost = float.Parse(theOrder.ServiceCost.ToString().Trim());
                }
                // Delete OrderDetail
                OrderDetaiProvider.DeleteOrderDetailOderByOrderID(OrderID);
                
                // Delete order
                OrderProvider.DeleteOderByOrderID(OrderID);

                // cập nhật vào tài khoản Customer phí dịch vụ
                ProfileBase theProfile = ProfileBase.Create(HttpContext.Current.User.Identity.Name);
                float serviceCostOfRegion = RegionProvider.GetRegionByRegionID(theOrder.ReceivedRegionID).ServiceCost; // phí dịch vụ theo miền
               
                
                // lay tai khoan hien tai trong currentBalance
                //Start
                CurrentBalance theCurrentBalance = AccountBalanceProvider.GetCurrentBalanceByCustomerID(theOrder.CustomerID);

                float toltalAccountCurrent = float.Parse(theCurrentBalance.TotalAmount.ToString()); // tổng tài khoản hiện tại
                float toltalAccountNow = toltalAccountCurrent - fServiceCost; // tổng tài khoảng thay thế
                AccountBalanceProvider.UpdateCurrentBalance(theOrder.CustomerID, toltalAccountNow);
                // End
                //theProfile.SetPropertyValue("ToltalAccount", toltalAccountNow);
                //theProfile.Save();
            }

            Response.Redirect("CustomerHistoryOfOrder.aspx");
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("CustomerHistoryOfOrder.aspx");
        }
    }
}